/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AppointmentPanel.java
 *
 * Created on Aug 12, 2011, 8:40:00 PM
 */
package panels;

import conn.EMIS4Connection;
import java.sql.SQLException;
import util.LoggedInUser;
import obj.Appointment;
import dao.AppointmentDao;
import dialog.ConfirmationDialog;
import java.util.List;
import javax.swing.JPanel;
import table.model.AppointmentTableModel;
import util.DateUtils;
import main.MainFrame;
import obj.Page;

/**
 *
 * @author John
 */
public class AppointmentPanel extends javax.swing.JPanel {

    private static AppointmentPanel instance;
    private AppointmentDao appointmentDao;
    private Appointment appt;
    private int apptID;
    private EditAppointmentPanel edit;
    private List<Appointment> allAppointments;
    private AppointmentTableModel todayApptTableModel;
    private AppointmentTableModel allApptTableModel;
    static int apptrows;
    DateUtils date;
    LoggedInUser loggedInUser = LoggedInUser.getInstance();
    EMIS4Connection conn = EMIS4Connection.getInstance();

    /** Creates new form AppointmentPanel */
    public AppointmentPanel() {
        super();
        initComponents();
        defineComponents();
        fillTableWithData();
    }

    private void defineComponents() {
        //m_tblAllAppointment    = new AppointmentTable();
        //m_tblTodaysAppointment = new AppointmentTable();

        todayApptTableModel = new AppointmentTableModel();
        allApptTableModel = new AppointmentTableModel();

        m_tblAllAppointment.setModel(allApptTableModel);
        m_tblTodaysAppointment.setModel(todayApptTableModel);

    }

    private void fillTableWithData() {
        conn.connect();



        try {
            appointmentDao = new AppointmentDao();
            appt = new Appointment();
            allAppointments = appointmentDao.loadAll(conn.getConnection());

            for (int index = 0; index < allAppointments.size(); index++) {
                appt = allAppointments.get(index);
                if (date.matchCurrentDate(appt.getScheduledDate())) {
                    todayApptTableModel.addAppoinetment(appt);
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        try {
            appointmentDao = new AppointmentDao();
            appt = new Appointment();
            allAppointments = appointmentDao.loadAll(conn.getConnection());

            for (int index = 0; index < allAppointments.size(); index++) {
                appt = allAppointments.get(index);
                allApptTableModel.addAppoinetment(appt);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }



    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        appointmentDao1 = new dao.AppointmentDao();
        appointmentDao2 = new dao.AppointmentDao();
        jDialog1 = new javax.swing.JDialog();
        Editor = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        m_tblTodaysAppointment = new table.AppointmentTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_tblAllAppointment = new table.AppointmentTable();

        jDialog1.setName("jDialog1"); // NOI18N
        jDialog1.getContentPane().setLayout(new java.awt.GridBagLayout());

        Editor.setName("Editor"); // NOI18N

        javax.swing.GroupLayout EditorLayout = new javax.swing.GroupLayout(Editor);
        Editor.setLayout(EditorLayout);
        EditorLayout.setHorizontalGroup(
            EditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        EditorLayout.setVerticalGroup(
            EditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(20, 15, 10, 15);
        jDialog1.getContentPane().add(Editor, gridBagConstraints);

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        m_tblTodaysAppointment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Doctor", "Patient", "Date/Time", "Status"
            }
        ));
        m_tblTodaysAppointment.setName("m_tblTodaysAppointment"); // NOI18N
        m_tblTodaysAppointment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                m_tblTodaysAppointmentMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                m_tblTodaysAppointmentMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(m_tblTodaysAppointment);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Appointment", jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        m_tblAllAppointment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Doctor", "Patient", "Date/Time", "Status"
            }
        ));
        m_tblAllAppointment.setName("m_tblAllAppointment"); // NOI18N
        m_tblAllAppointment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                m_tblAllAppointmentMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                m_tblAllAppointmentMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(m_tblAllAppointment);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("All Appointments", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void m_tblTodaysAppointmentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_m_tblTodaysAppointmentMouseClicked
        // TODO add your handling code here
        m_tblTodaysAppointment.getSelectedRow();
        MainFrame mainFrame = MainFrame.getInstance();
        Page page = new Page();

        mainFrame.setPanel(page);

    }//GEN-LAST:event_m_tblTodaysAppointmentMouseClicked

    private void m_tblAllAppointmentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_m_tblAllAppointmentMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_m_tblAllAppointmentMouseClicked

    private void m_tblTodaysAppointmentMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_m_tblTodaysAppointmentMouseReleased
        // TODO add your handling code here:
        m_tblTodaysAppointment.getSelectedRow();

        ConfirmationDialog confirmationDialog = new ConfirmationDialog(null, new EditAppointmentPanel(), true);
        confirmationDialog.setVisible(true);
    }//GEN-LAST:event_m_tblTodaysAppointmentMouseReleased

    private void m_tblAllAppointmentMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_m_tblAllAppointmentMouseReleased
        // TODO add your handling code here:
        m_tblAllAppointment.getSelectedRow();
        Editor = new EditAppointmentPanel();
        Editor.setVisible(true);
        if (Editor.get)
    }//GEN-LAST:event_m_tblAllAppointmentMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Editor;
    private dao.AppointmentDao appointmentDao1;
    private dao.AppointmentDao appointmentDao2;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private table.AppointmentTable m_tblAllAppointment;
    private table.AppointmentTable m_tblTodaysAppointment;
    // End of variables declaration//GEN-END:variables
}
